﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace AcctrueWMS.Foundation.Data
{
    /// <summary>
    /// 生成XmlDocument格式的DataModel描述文档
    /// </summary>
    public class XmlDataModelGenerator : IDataModelGenerator
    {
        public object Generate(DataModel model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement modelNode = xmlDoc.CreateElement("DataModel");

            //生成模型名
            XmlAttribute nameAttr = xmlDoc.CreateAttribute("name");
            nameAttr.Value = model.Name;
            modelNode.Attributes.Append(nameAttr);

            //显示名称
            XmlAttribute displayNameAttr = xmlDoc.CreateAttribute("displayName");
            displayNameAttr.Value = model.DisplayName;
            modelNode.Attributes.Append(displayNameAttr);

            //物理表名
            XmlAttribute physicalNameAttr = xmlDoc.CreateAttribute("physicalName");
            physicalNameAttr.Value = model.physicalName.ToString();
            modelNode.Attributes.Append(physicalNameAttr);

            //数据类型
            XmlAttribute dataTypeAttr = xmlDoc.CreateAttribute("logicalType");
            dataTypeAttr.Value = model.DataType.ToString();
            modelNode.Attributes.Append(dataTypeAttr);

            //如果物理类型是逻辑类型，则产生命令属性
            if (string.IsNullOrEmpty(model.physicalName) || model.physicalName=="")
            {
                XmlCDataSection generatingdCommandElt = xmlDoc.CreateCDataSection("generatingCommand");
                generatingdCommandElt.InnerText = model.GeneratingCommand;
                modelNode.AppendChild(generatingdCommandElt);
            }

            XmlDataModelColumnGenerator colGenerator = new XmlDataModelColumnGenerator();
            //分析数据模型的列
            foreach (DataModelColumn col in model.Columns)
            {
                modelNode.AppendChild(colGenerator.GenerateNode(xmlDoc, col));
            }
            return xmlDoc;
        }
    }

    public class XmlDataModelColumnGenerator
    {
        /// <summary>
        /// 为数据模型的定义文件增加每列的描述
        /// </summary>
        /// <param name="modelNode">xml文档的模型节点</param>
        /// <param name="col">列</param>
        /// <returns>新增的列描述节点</returns>
        public XmlNode GenerateNode(XmlNode modelNode, DataModelColumn col)
        {
            XmlElement elt = modelNode.OwnerDocument.CreateElement("Column");

            //名称节点
            XmlAttribute nameAttr = modelNode.OwnerDocument.CreateAttribute("name");
            nameAttr.Value = col.Name;
            modelNode.Attributes.Append(nameAttr);

            //显示名节点
            XmlAttribute displayNameAttr = modelNode.OwnerDocument.CreateAttribute("displayName");
            displayNameAttr.Value = col.DisplayName;
            modelNode.Attributes.Append(displayNameAttr);

            //类型节点
            XmlAttribute typeAttr = modelNode.OwnerDocument.CreateAttribute("columnType");
            typeAttr.Value = col.ColumnType.ToString();
            modelNode.AppendChild(typeAttr);

            //长度节点
            XmlAttribute lengthAttr = modelNode.OwnerDocument.CreateAttribute("length");
            lengthAttr.Value = col.MaxLength.ToString();
            modelNode.AppendChild(lengthAttr);

            //是否可为空
            XmlAttribute nullableAttr = modelNode.OwnerDocument.CreateAttribute("nullable");
            nullableAttr.Value = col.Nullable.ToString();
            modelNode.AppendChild(nullableAttr);

            return modelNode;
        }
    }
}
